package com.ruoyi.app.controller.business.neworder;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.app.controller.business.neworder.vo.AppParam;
import com.ruoyi.app.controller.business.neworder.vo.AppProcessVO;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.order.domain.JbApprovalLog;
import com.ruoyi.business.order.domain.JbOrder;
import com.ruoyi.business.rule.domain.JbLogisticsRules;
import com.ruoyi.business.rule.service.JbLogisticsRulesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.controller.business.goods.factory.JbCommodityFactory;
import com.ruoyi.web.controller.business.order.factory.JbOrderFactory;
import com.ruoyi.web.controller.business.order.vo.JbOrderVO;
import com.ruoyi.web.controller.business.statistics.factory.JbCommissionFactory;
import com.ruoyi.web.controller.business.statistics.vo.JbGrossProfitRateQueryVO;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 业务订单
 *
 * @author Jyl
 */
@Log4j2
@RestController
@RequestMapping("/app/neworder/my")
public class AppMyOrderController extends JoController
{

    @Autowired
    private JbOrderFactory jbOrderFactory;
    @Autowired
    private JbCommodityFactory jbCommodityFactory;
    @Autowired
    private JbCommissionFactory jbCommissionFactory;
    @Autowired
    private JbLogisticsRulesService jbLogisticsRulesService;

    /**
     * 置顶操作
     */
    @PutMapping("/top")
    public AjaxResult top(@RequestBody JbOrderVO jbOrderVO)
    {
        return success(jbOrderFactory.top(jbOrderVO));
    }

    /**
     * 置顶操作 取消
     */
    @PutMapping("/unTop")
    public AjaxResult unTop(@RequestBody JbOrderVO jbOrderVO)
    {
        return success(jbOrderFactory.unTop(jbOrderVO));
    }

    /**
     * 商品折算价格 根据当前登录人部门计算
     */
    @PostMapping("/conversion")
    public AjaxResult conversion(@RequestBody AppParam appParam)
    {
        return JoR.success(jbCommodityFactory.conversion(appParam.getIds()));
    }

    /**
     * 计算 订单提成
     */
    @PostMapping(value = "fnRoyalties")
    public AjaxResult fnRoyalties(@Valid @RequestBody JbGrossProfitRateQueryVO queryVO)
    {
        return success(jbCommissionFactory.fnRoyalties(queryVO));
    }

    /**
     * 查询 流程信息
     */
    @GetMapping("/findProcess")
    public AjaxResult findProcess(String processId)
    {
        JSONObject jsonObject = jbOrderFactory.getService().findProcess(processId);
        if (ObjectUtil.isEmpty(jsonObject))
        {
            throw new ServiceException("当前订单还未发起流程,请提交后再查看");
        }

        AppProcessVO processVO = JSONObject.parseObject(jsonObject.toJSONString(), AppProcessVO.class);
        return JoR.success(processVO);
    }

    /**
     * 结束
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping("/end")
    public AjaxResult end(@RequestBody JbApprovalLog jbApprovalLog)
    {
        return toAjax(jbOrderFactory.end(jbApprovalLog));
    }

    /**
     * 我的订单列表
     */
    @GetMapping("/list")
    public JSONObject list(PageParam pageParam, JbOrder jbOrder)
    {
        //查询我的订单
        jbOrder.setCreateBy(getUserId());

        IPage<JbOrderVO> iPage = jbOrderFactory.list(pageParam, jbOrder);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(JoR.rows(iPage)));
        jsonObject.putAll(jbOrderFactory.fnAmount(iPage.getRecords()));
        jsonObject.put("rows", iPage.getRecords());
        return jsonObject;
    }

    /**
     * 查询物流规则列表
     */
    @GetMapping("/logisticsList")
    public TableDataInfo logisticsList(PageParam page, JbLogisticsRules jbLogisticsRules)
    {
        LambdaQueryWrapper<JbLogisticsRules> select = new LambdaQueryWrapper<>();
        select.eq(JbLogisticsRules::isStatus, true);
        IPage<JbLogisticsRules> iPage = jbLogisticsRulesService.page(page.b(), select);
        return JoR.rows(iPage);
    }
}
